import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/index.vue'
import layout from '@/views/layout/index.vue'
import store from '@/store'
import Index from '@/views/index/index.vue'
import User from '@/views/user/user.vue'
import jiaose from '@/views/Role list/index.vue'
import jiaoseTwo from '@/views/Role list/index2.vue'
import Goodslist from '@/views/goods/goodslist.vue'
import Add from '@/views/goods/add.vue'
import GoodsType from '@/views/goods/goodsType.vue'
import Goodsclass from '@/views/goods/goodsclass.vue'
import Order from '@/views/order/index.vue'
import Data from '@/views/data report/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: login
  },
  {
    path: '/',
    component: layout,
    require: 'index',
    children: [
      {
        path: 'index',
        component: Index
      },
      {
        path: 'user',
        component: User
      },
      {
        path: 'jiaose',
        component: jiaose
      },
      {
        path: 'jiaose2',
        component: jiaoseTwo
      },
      {
        path: 'goodslist',
        component: Goodslist
      },
      {
        path: 'add',
        component: Add
      },
      {
        path: 'goodstype',
        component: GoodsType
      },
      {
        path: 'goodsclass',
        component: Goodsclass
      },
      {
        path: 'order',
        component: Order
      },
      {
        path: 'data',
        component: Data
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path !== '/login' && !token) return next('/login')
  next()
})
export default router
